<?php

use Hashids\Hashids;
use think\Loader;

/**
 * 获取配置信息
 * @param $name
 * @return array
 * @author wb <pithyone@vip.qq.com>
 */
function c($name)
{
    if ($name == '') return [];

    $Config = Loader::model('Config', 'logic');
    $ret = $Config::val($name);

    return $ret;
}

/**
 * 周次
 * @param $time
 * @return mixed
 * @author wb <pithyone@vip.qq.com>
 */
function get_week($time)
{
    $week_array = array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    $week = $week_array[date("w", strtotime($time))];
    return $week;
}

/**
 * 显示最近时间
 * @param $time
 * @return bool|string
 * @author wb <pithyone@vip.qq.com>
 */
function recent_time($time)
{
    $t_diff = time() - $time;

    if ($t_diff <= 0) return date('m-d', $time);

    $format = [
        '31536000' => '年',
        '86400' => '天',
        '3600' => '小时',
        '60' => '分钟',
        '1' => '秒'
    ];

    foreach ($format as $k => $v) {
        $day = floor($t_diff / (int)$k);

        if ($day == 0)
            continue;

        //60s之内显示刚刚
        if ($v == '秒')
            return '刚刚';

        //大于1天
        if ($v == '天' && $day == 1)
            return '昨天';

        //大于2天
        if ($v == '天' && $day == 2)
            return '前天';

        //大于3天
        if ($v == '天' && $day > 2)
            return date('m-d', $time);

        //大于1年
        if ($v == '年' && $day > 0)
            return date('y-m-d', $time);

        return $day . $v . '前';
    }
    return '未知时间';
}

/**
 * 返回时间差对应的字符串描述
 * @param $time int
 * @return string
 */
function time_diff2string($time)
{
    $f = array(
        '31536000' => '年',
        '2592000' => '个月',
        '604800' => '星期',
        '86400' => '天',
        '3600' => '时',
        '60' => '分',
        '1' => '秒'
    );
    foreach ($f as $k => $v) {
        if (0 != $c = floor($time / (int)$k)) {
            $m = floor($time % $k);
            foreach ($f as $x => $y) {
                if (0 != $r = floor($m / (int)$x)) {
                    return $c . $v . $r . $y;
                }
            }
            return $c . $v;
        }
    }
    return '';
}

/**
 * 返回最近一周的日期
 * @return array
 * @author wb <pithyone@vip.qq.com>
 */
function get_7days()
{
    $days = array();
    for ($i = 0; $i < 7; $i++) {
        $d = '-' . $i . ' day';
        $time = date('Y-m-d', strtotime($d));
        array_push($days, $time);
    }
    return $days;
}

/**
 * 返回时间差对应的小时
 * @param $time int
 * @return string
 */
function time_diff2hour($time)
{
    $f = array(
        '3600' => '时',
        '60' => '分',
        '1' => '秒'
    );
    foreach ($f as $k => $v) {
        if (0 != $c = floor($time / (int)$k)) {
            $m = floor($time % $k);
            foreach ($f as $x => $y) {
                if (0 != $r = floor($m / (int)$x)) {
                    return $c . $v . $r . $y;
                }
            }
            return $c . $v;
        }
    }
    return '';
}

/**
 * 随机数
 * @param $count
 * @return string
 * @author wb <pithyone@vip.qq.com>
 */
function get_rand($count)
{
    $randStr = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
    $rand = substr($randStr, 0, $count);
    return $rand;
}

/**
 * 搜索二维数组
 * @param $k
 * @param $v
 * @param $array
 * @return int|null|string
 * @author wb <pithyone@vip.qq.com>
 */
function search_2d_array($k, $v, $array)
{
    foreach ($array as $key => $val) {
        if ($val[$k] === $v) {
            return $key;
        }
    }
    return null;
}

/**
 * 生成hashid
 * @return bool|string
 * @author wb <pithyone@vip.qq.com>
 */
function get_hashid()
{
    $hashids = new Hashids();
    $micro = microtime();
    $id = $hashids->encode(substr($micro, 11), substr($micro, 2, 6), rand(1000, 9999));
    return $id;
}

/**
 * 无限极分类
 * @param $list
 * @param string $root
 * @param string $id
 * @param string $pid
 * @param string $child
 * @return array
 */
function list2tree($list, $root = '', $id = 'id', $pid = 'pid', $child = 'child')
{
    $temp = [];
    $tree = [];

    foreach ($list as $val) {
        $temp[$val[$id]] = $val->toArray();
    }

    foreach ($temp as $key => $val) {
        if ($val[$pid] == $root) $tree[] = &$temp[$key];
        else $temp[$val[$pid]][$child][] = &$temp[$key];
    }

    return $tree;
}
